package com.hsgene.order.queue;

import java.util.Objects;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author wxf
 * @date 2019/1/3 14:00
 **/
public class OrderQueueInfo implements Delayed {

    /**
     * 订单id
     */
    private String orderId;
    /**
     * 过期时间
     */
    private long startTime;

    public OrderQueueInfo(){

    }

    public OrderQueueInfo(String orderId, long startTime) {
        this.orderId = orderId;
        this.startTime = startTime;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return startTime - System.currentTimeMillis();
    }

    @Override
    public int compareTo(Delayed o) {
        if (o == this) {
            return 0;
        }
        if (o instanceof OrderQueueInfo) {
            OrderQueueInfo otherRequest = (OrderQueueInfo) o;
            long otherStartTime = otherRequest.getStartTime();
            return (int) (this.startTime - otherStartTime);
        }
        return 0;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        OrderQueueInfo queueInfo = (OrderQueueInfo) o;
        return Objects.equals(orderId, queueInfo.orderId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(orderId);
    }
}
